<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>2.2 服务提供者 | weiyonghua&#39;s Blog | YOUR_SUBTITLE</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="springclould">
    <meta name="description" content="2.2 服务提供者首先说明一下，为了便于讲解，本节之后，如无特殊说明，均是以单点的Eureka进行讲解的。 服务提供者和服务消费者下面这张表格，简单描述了服务提供者/消费者是什么：    名词 概念     服务提供者 服务的被调用方（即：为其他服务提供服务的服务）   服务消费者 服务的调用方（即：依赖其他服务的服务）    服务提供者代码示例这是一个稍微有点复杂的程序。我们使用spring-d">
<meta name="keywords" content="springclould">
<meta property="og:type" content="article">
<meta property="og:title" content="2.2 服务提供者">
<meta property="og:url" content="weiyonghua's Blog/2017/07/03/2-2-服务提供者/index.html">
<meta property="og:site_name" content="weiyonghua&#39;s Blog">
<meta property="og:description" content="2.2 服务提供者首先说明一下，为了便于讲解，本节之后，如无特殊说明，均是以单点的Eureka进行讲解的。 服务提供者和服务消费者下面这张表格，简单描述了服务提供者/消费者是什么：    名词 概念     服务提供者 服务的被调用方（即：为其他服务提供服务的服务）   服务消费者 服务的调用方（即：依赖其他服务的服务）    服务提供者代码示例这是一个稍微有点复杂的程序。我们使用spring-d">
<meta property="og:image" content="weiyonghua%27s%20Blog/2017/07/03/2-2-服务提供者/images/eureka-with-provider.png">
<meta property="og:updated_time" content="2017-07-03T08:32:25.441Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="2.2 服务提供者">
<meta name="twitter:description" content="2.2 服务提供者首先说明一下，为了便于讲解，本节之后，如无特殊说明，均是以单点的Eureka进行讲解的。 服务提供者和服务消费者下面这张表格，简单描述了服务提供者/消费者是什么：    名词 概念     服务提供者 服务的被调用方（即：为其他服务提供服务的服务）   服务消费者 服务的调用方（即：依赖其他服务的服务）    服务提供者代码示例这是一个稍微有点复杂的程序。我们使用spring-d">
<meta name="twitter:image" content="weiyonghua%27s%20Blog/2017/07/03/2-2-服务提供者/images/eureka-with-provider.png">
    
        <link rel="alternative" href="/atom.xml" title="weiyonghua&#39;s Blog" type="application/atom+xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.4.9">
    <script>window.lazyScripts=[]</script>
</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">weiyonghua</h5>
          <a href="mailto:weiyonghua16@gmail.com" title="weiyonghua16@gmail.com" class="mail">weiyonghua16@gmail.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/1135831485" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://t.qq.com/fy1135831485" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">2.2 服务提供者</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">2.2 服务提供者</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-07-03T08:21:00.000Z" itemprop="datePublished" class="page-time">
  2017-07-03
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/springclould专栏/">springclould专栏</a></li></ul>

            
        </h5>
    </div>

    

</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#2-2-服务提供者"><span class="post-toc-number">1.</span> <span class="post-toc-text">2.2 服务提供者</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#服务提供者和服务消费者"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">服务提供者和服务消费者</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#服务提供者代码示例"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">服务提供者代码示例</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#测试"><span class="post-toc-number">1.2.1.</span> <span class="post-toc-text">测试</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#服务注册到高可用Eureka"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">服务注册到高可用Eureka</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#代码地址（任选其一）"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">代码地址（任选其一）</span></a></li></ol></li></ol>
        </nav>
    </aside>
    
<article id="post-2-2-服务提供者"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">2.2 服务提供者</h1>
        <div class="post-meta">
            <time class="post-time" title="2017年07月03日 16:21" datetime="2017-07-03T08:21:00.000Z"  itemprop="datePublished">2017-07-03</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/springclould专栏/">springclould专栏</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="2-2-服务提供者"><a href="#2-2-服务提供者" class="headerlink" title="2.2 服务提供者"></a>2.2 服务提供者</h1><p>首先说明一下，为了便于讲解，本节之后，如无特殊说明，均是以单点的Eureka进行讲解的。</p>
<h2 id="服务提供者和服务消费者"><a href="#服务提供者和服务消费者" class="headerlink" title="服务提供者和服务消费者"></a>服务提供者和服务消费者</h2><p>下面这张表格，简单描述了服务提供者/消费者是什么：</p>
<table>
<thead>
<tr>
<th>名词</th>
<th>概念</th>
</tr>
</thead>
<tbody>
<tr>
<td>服务提供者</td>
<td>服务的被调用方（即：为其他服务提供服务的服务）</td>
</tr>
<tr>
<td>服务消费者</td>
<td>服务的调用方（即：依赖其他服务的服务）</td>
</tr>
</tbody>
</table>
<h2 id="服务提供者代码示例"><a href="#服务提供者代码示例" class="headerlink" title="服务提供者代码示例"></a>服务提供者代码示例</h2><p>这是一个稍微有点复杂的程序。我们使用spring-data-jpa操作h2数据库，同时将该服务注册到注册中心Eureka中。</p>
<ul>
<li>创建一个Maven工程，并在pom.xml中添加如下内容：</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></div><div class="line">  <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</div><div class="line">  <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>microservice-provider-user<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">packaging</span>&gt;</span>jar<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">parent</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.itmuch.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-microservice-study<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">parent</span>&gt;</span></div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></div><div class="line">    <span class="comment">&lt;!-- 添加Eureka的依赖 --&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"></div><div class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-jpa<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"></div><div class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.h2database<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>h2<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"></div><div class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-actuator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></div></pre></td></tr></table></figure>
<ul>
<li>配置文件：application.yml</li>
</ul>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line"><span class="attr">server:</span></div><div class="line"><span class="attr">  port:</span> <span class="number">8000</span></div><div class="line"><span class="attr">spring:</span></div><div class="line"><span class="attr">  application:</span></div><div class="line"><span class="attr">    name:</span> <span class="string">microservice-provider-user</span>    <span class="comment"># 项目名称尽量用小写</span></div><div class="line"><span class="attr">  jpa:</span></div><div class="line"><span class="attr">    generate-ddl:</span> <span class="literal">false</span></div><div class="line"><span class="attr">    show-sql:</span> <span class="literal">true</span></div><div class="line"><span class="attr">    hibernate:</span></div><div class="line"><span class="attr">      ddl-auto:</span> <span class="string">none</span></div><div class="line"><span class="attr">  datasource:</span>                           <span class="comment"># 指定数据源</span></div><div class="line"><span class="attr">    platform:</span> <span class="string">h2</span>                        <span class="comment"># 指定数据源类型</span></div><div class="line"><span class="attr">    schema:</span> <span class="attr">classpath:schema.sql</span>        <span class="comment"># 指定h2数据库的建表脚本</span></div><div class="line"><span class="attr">    data:</span> <span class="attr">classpath:data.sql</span>            <span class="comment"># 指定h2数据库的insert脚本</span></div><div class="line"><span class="attr">logging:</span></div><div class="line"><span class="attr">  level:</span></div><div class="line"><span class="attr">    root:</span> <span class="string">INFO</span></div><div class="line">    <span class="string">org.hibernate:</span> <span class="string">INFO</span></div><div class="line">    <span class="string">org.hibernate.type.descriptor.sql.BasicBinder:</span> <span class="string">TRACE</span></div><div class="line">    <span class="string">org.hibernate.type.descriptor.sql.BasicExtractor:</span> <span class="string">TRACE</span></div><div class="line">    <span class="string">com.itmuch.youran.persistence:</span> <span class="string">ERROR</span></div><div class="line"><span class="attr">eureka:</span></div><div class="line"><span class="attr">  client:</span></div><div class="line"><span class="attr">    serviceUrl:</span></div><div class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://discovery:8761/eureka/</span>    <span class="comment"># 指定注册中心的地址</span></div><div class="line"><span class="attr">  instance:</span></div><div class="line"><span class="attr">    preferIpAddress:</span> <span class="literal">true</span></div></pre></td></tr></table></figure>
<ul>
<li>建表语句：schema.sql</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">drop</span> <span class="keyword">table</span> <span class="keyword">user</span> <span class="keyword">if</span> <span class="keyword">exists</span>;</div><div class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">user</span> (<span class="keyword">id</span> <span class="built_in">bigint</span> <span class="keyword">generated</span> <span class="keyword">by</span> <span class="keyword">default</span> <span class="keyword">as</span> <span class="keyword">identity</span>, username <span class="built_in">varchar</span>(<span class="number">255</span>), age <span class="built_in">int</span>, primary <span class="keyword">key</span> (<span class="keyword">id</span>));</div></pre></td></tr></table></figure>
<ul>
<li>插库语句：data.sql</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span> (<span class="keyword">id</span>, username, age) <span class="keyword">values</span> (<span class="number">1</span>,<span class="string">'Tom'</span>,<span class="number">12</span>);</div><div class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span> (<span class="keyword">id</span>, username, age) <span class="keyword">values</span> (<span class="number">2</span>,<span class="string">'Jerry'</span>, <span class="number">23</span>);</div><div class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span> (<span class="keyword">id</span>, username, age) <span class="keyword">values</span> (<span class="number">3</span>,<span class="string">'Reno'</span>, <span class="number">44</span>);</div><div class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span> (<span class="keyword">id</span>, username, age) <span class="keyword">values</span> (<span class="number">4</span>,<span class="string">'Josh'</span>, <span class="number">55</span>);</div></pre></td></tr></table></figure>
<ul>
<li>DAO：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Repository</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserRepository</span> <span class="keyword">extends</span> <span class="title">JpaRepository</span>&lt;<span class="title">User</span>, <span class="title">Long</span>&gt; </span>&#123;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>Controller：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * 作用：</div><div class="line"> * ① 测试服务实例的相关内容</div><div class="line"> * ② 为后来的服务做提供</div><div class="line"> * <span class="doctag">@author</span> eacdy</div><div class="line"> */</div><div class="line"><span class="meta">@RestController</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>&#123;</div><div class="line">  <span class="meta">@Autowired</span></div><div class="line">  <span class="keyword">private</span> DiscoveryClient discoveryClient;</div><div class="line">  <span class="meta">@Autowired</span></div><div class="line">  <span class="keyword">private</span> UserRepository userRepository;</div><div class="line"></div><div class="line">  <span class="comment">/**</span></div><div class="line">   * 注：<span class="doctag">@GetMapping</span>("/&#123;id&#125;")是spring 4.3的新注解等价于：</div><div class="line">   * <span class="doctag">@RequestMapping</span>(value = "/id", method = RequestMethod.GET)</div><div class="line">   * 类似的注解还有<span class="doctag">@PostMapping</span>等等</div><div class="line">   * <span class="doctag">@param</span> id</div><div class="line">   * <span class="doctag">@return</span> user信息</div><div class="line">   */</div><div class="line">  <span class="meta">@GetMapping</span>(<span class="string">"/&#123;id&#125;"</span>)</div><div class="line">  <span class="function"><span class="keyword">public</span> User <span class="title">findById</span><span class="params">(@PathVariable Long id)</span> </span>&#123;</div><div class="line">    User findOne = <span class="keyword">this</span>.userRepository.findOne(id);</div><div class="line">    <span class="keyword">return</span> findOne;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="comment">/**</span></div><div class="line">   * 本地服务实例的信息</div><div class="line">   * <span class="doctag">@return</span></div><div class="line">   */</div><div class="line">  <span class="meta">@GetMapping</span>(<span class="string">"/instance-info"</span>)</div><div class="line">  <span class="function"><span class="keyword">public</span> ServiceInstance <span class="title">showInfo</span><span class="params">()</span> </span>&#123;</div><div class="line">    ServiceInstance localServiceInstance = <span class="keyword">this</span>.discoveryClient.getLocalServiceInstance();</div><div class="line">    <span class="keyword">return</span> localServiceInstance;</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>实体类：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Entity</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</div><div class="line">  <span class="meta">@Id</span></div><div class="line">  <span class="meta">@GeneratedValue</span>(strategy = GenerationType.AUTO)</div><div class="line">  <span class="keyword">private</span> Long id;</div><div class="line">  <span class="meta">@Column</span></div><div class="line">  <span class="keyword">private</span> String username;</div><div class="line">  <span class="meta">@Column</span></div><div class="line">  <span class="keyword">private</span> Integer age;</div><div class="line">  ...</div><div class="line">  <span class="comment">// getters and setters</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>编写Spring Boot启动程序，通过@EnableDiscoveryClient注解，即可将microservice-provider-user服务注册到Eureka上面去</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@SpringBootApplication</span></div><div class="line"><span class="meta">@EnableDiscoveryClient</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserApplication</span> </span>&#123;</div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">    SpringApplication.run(UserApplication.class, args);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>至此，代码编写完成。</p>
<h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><p>我们依次启动Eureka服务和microservice-provider-user服务。</p>
<p>访问：<a href="http://localhost:8761" target="_blank" rel="external">http://localhost:8761</a>，如下图。我们会发现microservice-provider-user服务已经被注册到了Eureka上面了。</p>
<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="images/eureka-with-provider.png" alt="user-service服务注册到Eureka上" title="">
                </div>
                <div class="image-caption">user-service服务注册到Eureka上</div>
            </figure>
<p>访问：<a href="http://localhost:8000/instance-info" target="_blank" rel="external">http://localhost:8000/instance-info</a>，返回结果：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">    <span class="attr">"host"</span>: <span class="string">"192.168.0.59"</span>,</div><div class="line">    <span class="attr">"port"</span>: <span class="number">8000</span>,</div><div class="line">    <span class="attr">"metadata"</span>: &#123;&#125;,</div><div class="line">    <span class="attr">"uri"</span>: <span class="string">"http://192.168.0.59:8000"</span>,</div><div class="line">    <span class="attr">"secure"</span>: <span class="literal">false</span>,</div><div class="line">    <span class="attr">"serviceId"</span>: <span class="string">"microservice-provider-user"</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>访问：<a href="http://discovery:8000/1" target="_blank" rel="external">http://discovery:8000/1</a>，返回结果：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line">    <span class="attr">"id"</span>: <span class="number">1</span>,</div><div class="line">    <span class="attr">"username"</span>: <span class="string">"Tom"</span>,</div><div class="line">    <span class="attr">"age"</span>: <span class="number">12</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="服务注册到高可用Eureka"><a href="#服务注册到高可用Eureka" class="headerlink" title="服务注册到高可用Eureka"></a>服务注册到高可用Eureka</h2><p>如果<code>Eureka</code> 是高可用的，那么各个微服务配置只需要将<code>defaultZone</code> 改为如下即可：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="attr">eureka:</span></div><div class="line"><span class="attr">  client:</span></div><div class="line"><span class="attr">    serviceUrl:</span></div><div class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://peer1:8761/eureka/,http://peer2:8762/eureka</span></div></pre></td></tr></table></figure>
<h2 id="代码地址（任选其一）"><a href="#代码地址（任选其一）" class="headerlink" title="代码地址（任选其一）"></a>代码地址（任选其一）</h2><blockquote>
<p><a href="http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-provider-user" target="_blank" rel="external">http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-provider-user</a><br><a href="https://github.com/eacdy/spring-cloud-study/tree/master/microservice-provider-user" target="_blank" rel="external">https://github.com/eacdy/spring-cloud-study/tree/master/microservice-provider-user</a></p>
</blockquote>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-07-03T08:32:25.441Z" itemprop="dateUpdated">2017年7月3日 16:32</time>
</span><br>


        这里写留言或版权声明：<a href="/2017/07/03/2-2-服务提供者/" target="_blank" rel="external">weiyonghua&#39;s Blog/2017/07/03/2-2-服务提供者/</a>
    </div>
    <footer>
        <a href="weiyonghua's Blog">
            <img src="/img/avatar.jpg" alt="weiyonghua">
            weiyonghua
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/springclould/">springclould</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=weiyonghua's Blog/2017/07/03/2-2-服务提供者/&title=《2.2 服务提供者》 — weiyonghua's Blog&pic=weiyonghua's Blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=weiyonghua's Blog/2017/07/03/2-2-服务提供者/&title=《2.2 服务提供者》 — weiyonghua's Blog&source=YOUR_DESC" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=weiyonghua's Blog/2017/07/03/2-2-服务提供者/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《2.2 服务提供者》 — weiyonghua's Blog&url=weiyonghua's Blog/2017/07/03/2-2-服务提供者/&via=weiyonghua's Blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=weiyonghua's Blog/2017/07/03/2-2-服务提供者/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/07/03/2-3-服务消费者/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">2.3 服务消费者</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/07/03/2-1-6-Consul高可用/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">2.1.6 Consul高可用</h4>
      </a>
    </div>
  
</nav>



    

<div class="comments" id="comments">
    <div class="ds-thread" data-thread-key="2-2-服务提供者" data-title="2.2 服务提供者" data-url="weiyonghua's Blog/2017/07/03/2-2-服务提供者/"></div>
</div>
<script>
lazyScripts.push('//cdn.bootcss.com/marked/0.3.6/marked.min.js');

var duoshuoQuery = {short_name:'ysblog', theme: 'none'};
lazyScripts.push('/js/embed.min.js?v=1.4.9');


</script>







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <ul class="reward-items">
        
        <li>
            <img src="/img/wechat.jpg" title="微信打赏二维码" alt="微信打赏二维码">
            <p>微信</p>
        </li>
        

        
        <li>
            <img src="/img/alipay.jpg" title="支付宝打赏二维码" alt="支付宝打赏二维码">
            <p>支付宝</p>
        </li>
        
    </ul>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            <span>博客内容遵循 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p>
            <span>Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a></span>
            <span>weiyonghua's Blog &copy; 2015 - 2017</span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=weiyonghua's Blog/2017/07/03/2-2-服务提供者/&title=《2.2 服务提供者》 — weiyonghua's Blog&pic=weiyonghua's Blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=weiyonghua's Blog/2017/07/03/2-2-服务提供者/&title=《2.2 服务提供者》 — weiyonghua's Blog&source=YOUR_DESC" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=weiyonghua's Blog/2017/07/03/2-2-服务提供者/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《2.2 服务提供者》 — weiyonghua's Blog&url=weiyonghua's Blog/2017/07/03/2-2-服务提供者/&via=weiyonghua's Blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=weiyonghua's Blog/2017/07/03/2-2-服务提供者/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };



</script>

<script src="/js/main.min.js?v=1.4.9"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.4.9" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>


</body>
</html>
